package com.service.impl;

import com.constant.MqttMessageType;
import com.dao.DeviceBoxMapper;
import com.dao.PlacedHospitalRelationProductMapper;
import com.dao.ProductMapper;
import com.dao.VenderDeviceMapper;
import com.emqx.MqttMessageDTO;
import com.emqx.MqttMessageSender;
import com.exception.BusinessException;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.model.PlacedHospitalRelationProduct;
import com.model.Product;
import com.model.VenderDevice;
import com.page.GenericPage;
import com.po.PlacedHospitalRelationProductQueryPO;
import com.po.PlacedHospitalRelationProductSaveOrUpdatePO;
import com.result.Result;
import com.service.PlacedHospitalRelationProductService;
import com.utils.DateUtil;
import com.vo.PlacedHospitalRelationProductVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class PlacedHospitalRelationProductServiceImpl implements PlacedHospitalRelationProductService {

    @Resource
    PlacedHospitalRelationProductMapper placedHospitalRelationProductMapper;

    @Resource
    DeviceBoxMapper deviceBoxMapper;

    @Resource
    VenderDeviceMapper venderDeviceMapper;

    @Resource
    ProductMapper productMapper;



    @Autowired
    private MqttMessageSender mqttMessageSender;

    @Override
    @Transactional
    public Result<Boolean> saveOrUpdate(PlacedHospitalRelationProductSaveOrUpdatePO record) {

        if (ObjectUtils.isEmpty(record.getHospitalId())||ObjectUtils.isEmpty(record.getProductId()) ||
                ObjectUtils.isEmpty(record.getFactPrice()) || record.getFactPrice().doubleValue() < 0.01 ){
            throw  new BusinessException("参数异常");
        }
        PlacedHospitalRelationProduct placedHospitalRelationProduct = placedHospitalRelationProductMapper.selectByHospitalIdAndProductId(record.getHospitalId(),record.getProductId());

        if (ObjectUtils.isEmpty(placedHospitalRelationProduct)){
           //添加
            placedHospitalRelationProduct = new PlacedHospitalRelationProduct();
            placedHospitalRelationProduct.setHospitalId(record.getHospitalId());
            placedHospitalRelationProduct.setProductId(record.getProductId());
//            placedHospitalRelationProduct.setAgent(record.getAgent());
            placedHospitalRelationProduct.setFactPrice(record.getFactPrice());
           placedHospitalRelationProductMapper.insert(placedHospitalRelationProduct);
        }else{
            placedHospitalRelationProduct.setFactPrice(record.getFactPrice());
            placedHospitalRelationProductMapper.updateByPrimaryKeySelective(placedHospitalRelationProduct);
        }

        VenderDevice condition = new VenderDevice();
        condition.setHospitalId(record.getHospitalId());
        List<VenderDevice> venderDevices = venderDeviceMapper.selectByCondition(condition);
        for (VenderDevice device:venderDevices){
                    deviceBoxMapper.unifyProductFackPrice(device.getId(),record.getProductId(),record.getFactPrice());
        }

        //发送通知
        Date now = new Date();
        MqttMessageDTO message=new MqttMessageDTO();
        message.setMsgSender("server_in");
        message.setMsgReceiver("server_out");
        message.setMsgType(MqttMessageType.FLUSH_PRODUCT_LIST);
        message.setMsgBody("");
        message.setSendTime(now);
        mqttMessageSender.send(message);


        return new Result<>().ok();
    }

    @Override
    public Result<Boolean> modifyById(PlacedHospitalRelationProduct record) {
        if (ObjectUtils.isEmpty(record.getId()) ||
                ObjectUtils.isEmpty(record.getFactPrice()) || record.getFactPrice().doubleValue() < 0.01 ){
            throw  new BusinessException("参数异常");
        }
        record.setHospitalId(null);
        record.setProductId(null);
        placedHospitalRelationProductMapper.updateByPrimaryKeySelective(record);
        return new Result<>().ok();
    }

    @Override
    public Result<PlacedHospitalRelationProductVO> queryById(Long id) {
        PlacedHospitalRelationProduct relation = placedHospitalRelationProductMapper.selectByPrimaryKey(id);

        if (!ObjectUtils.isEmpty(relation)){
            Product product = productMapper.selectByPrimaryKey(relation.getProductId());
            PlacedHospitalRelationProductVO placedHospitalRelationProductVO = new PlacedHospitalRelationProductVO();
            BeanUtils.copyProperties(product,placedHospitalRelationProductVO);
            placedHospitalRelationProductVO.setFactPrice(relation.getFactPrice());
            placedHospitalRelationProductVO.setHospitalId(relation.getHospitalId());
            return new Result<PlacedHospitalRelationProductVO>().ok(placedHospitalRelationProductVO);

        }
        return new Result<PlacedHospitalRelationProduct>().ok();
    }

    @Override
    public Result<GenericPage<PlacedHospitalRelationProductVO>> queryAll(PlacedHospitalRelationProductQueryPO record) {
        if (ObjectUtils.isEmpty(record.getHospitalId())){
            throw new BusinessException("参数异常");
        }

        int pageNo = ObjectUtils.isEmpty(record.getPageNo())|| record.getPageNo()==0?1:record.getPageNo();
        int pageSize = ObjectUtils.isEmpty(record.getPageSize())|| record.getPageSize()==0?10:record.getPageSize();

        PageHelper.startPage(pageNo, pageSize);
        List<Product> products = productMapper.selectByCondition(new Product());

        List<PlacedHospitalRelationProductVO> list = new ArrayList<>();
        PlacedHospitalRelationProductVO placedHospitalRelationProductVO = null;
        for (Product product:products){
            placedHospitalRelationProductVO = new PlacedHospitalRelationProductVO();
            BeanUtils.copyProperties(product,placedHospitalRelationProductVO);
            PlacedHospitalRelationProduct relation = placedHospitalRelationProductMapper.selectByHospitalIdAndProductId(record.getHospitalId(), product.getId());
            if (!ObjectUtils.isEmpty(relation)){
                placedHospitalRelationProductVO.setFactPrice(relation.getFactPrice());
            }else{
                if (!ObjectUtils.isEmpty(product.getProductPrice())){
                    //售卖商品
                    placedHospitalRelationProductVO.setFactPrice(product.getProductPrice());
                }else if (!ObjectUtils.isEmpty(product.getRentPricePerDay()) || !ObjectUtils.isEmpty(product.getRentPricePerHour())){
                    //租赁商品
                    if (!ObjectUtils.isEmpty(product.getRentPricePerDay())){
                        //日租
                        placedHospitalRelationProductVO.setFactPrice(product.getRentPricePerDay());
                    }else {
                        //时租
                        placedHospitalRelationProductVO.setFactPrice(product.getRentPricePerHour());
                    }
                }

            }
            placedHospitalRelationProductVO.setHospitalId(record.getHospitalId());
//            placedHospitalRelationProductVO.setAgent(record.getAgent());
            list.add(placedHospitalRelationProductVO);
        }
        PageInfo<PlacedHospitalRelationProductVO> pageInfo = new PageInfo<>(list);
        GenericPage<PlacedHospitalRelationProductVO> genericPage = new GenericPage<PlacedHospitalRelationProductVO>(pageSize, pageInfo.getTotal(), list);
        return new Result<GenericPage<PlacedHospitalRelationProductVO>>().ok(genericPage);
    }
}
